package com.zisuyi.uilibrary.module.dialog.redeem


import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import com.weilele.mvvm.base.helper.annotation.MvvmVLayoutAdapter
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.utils.activity.dip
import com.weilele.mvvm.utils.fragment.arguments
import com.weilele.mvvm.utils.safeGet
import com.weilele.mvvm.utils.vLaout.VLayoutAdapter
import com.zisuyi.game.sdk.net.bean.UserInfoBean
import com.zisuyi.game.sdk.utils.UserData
import com.zisuyi.uilibrary.base.BaseUiDialog
import com.zisuyi.uilibrary.databinding.DialogRedeemBinding
import com.zisuyi.uilibrary.module.activty.redeem.RedeemHolder


class RedeemDialog : BaseUiDialog<DialogRedeemBinding>() {
    companion object {
        fun show(appCompatActivity: AppCompatActivity?, itemData: UserInfoBean.RedeemDTO?) {
            appCompatActivity ?: return
            RedeemDialog().apply {
                arguments {
                    putSerializable("RedeemDTO", itemData)
                }
            }.show(appCompatActivity)
        }
    }

    private val redeemDTO by lazy {
        arguments?.getSerializable("RedeemDTO").safeGet<UserInfoBean.RedeemDTO>()
    }


    @MvvmVLayoutAdapter
    private lateinit var taskHelper: VLayoutAdapter<RedeemDialogHolder.Bean, RedeemDialogHolder>

    override fun onRootViewLayoutParams(lp: FrameLayout.LayoutParams) {
        super.onRootViewLayoutParams(lp)
        lp.marginStart = dip(32)
        lp.marginEnd = lp.marginStart
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        redeemDTO?.let {
            RedeemHolder(mBinding.rcvRedeem).bindData(it)
        }
        mBinding.rcvRedeemTask.addAdapter(taskHelper)
        redeemDTO?.conditions?.let {
            taskHelper.refresh(
                    it.map { RedeemDialogHolder.Bean(it) }.toMutableList().also {
                        //最后一条新增钻石条件
                        it.add(RedeemDialogHolder.Bean(UserInfoBean.RedeemDTO.ConditionsDTO().apply {
                            this.message = "${it.count()+1}. Number of diamonds"
                            this.value = redeemDTO?.diamond?.toString() ?: "0"
                            this.complete = UserData.diamond.toString()
                        }))
                    }
            )
        }
    }

    override fun getClickView(): List<View?>? {
        return listOf(mBinding.ivDialogClose)
    }

    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            mBinding.ivDialogClose -> {
                dismiss()
            }
            else -> {
                RedeemEmailDialog.show(appCompatActivity, redeemDTO)
            }
        }
    }
}